package com.ddtj.demo1.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ddtj.demo1.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Map;

@Service
public class TokenService {
    @Value("${jwt.secret}")
    private String secret;
    @Value("${jwt.expireTime}")
    private Integer expireTime;
    public String getToken(LoginUser loginUser){
        return null;
    }

    public String getToken(Map<String,String> map){
        JWTCreator.Builder builder = JWT.create();
        map.forEach((K,V)->{
            builder.withClaim(K,V);
        });
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE,expireTime);
        builder.withExpiresAt(instance.getTime());
        String token = builder.sign(Algorithm.HMAC256(secret));
        return token;
    }
    public DecodedJWT verifyToken(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
        return verify;
    }
}
